//
//  CPBaseViewController.swift
//  cockpit
//
//  Created by Haijun Si on 2020/7/7.
//  Copyright © 2020 chenganrt. All rights reserved.
//

import UIKit
import CocoaLumberjack
import RxSwift
import RxCocoa
import NSObject_Rx
import RxDataSources
import RxOptional

class CPBaseViewController: UIViewController, UIGestureRecognizerDelegate {

    private var navi:WRNavigationBar?
    
    var isHidNavi:Bool? {
        didSet {
            self.navigationController?.setNavigationBarHidden(isHidNavi ?? false, animated: true)
        }
    }
    
    var navibackImg:String? {
        didSet {
            WRNavigationBar.defaultNavBarBackgroundImage = UIImage.init(named: navibackImg ?? "")
        }
    }
    
    var hidLeftBtn:Bool? {
        didSet {
            self.navigationItem.leftBarButtonItem?.customView?.isHidden = hidLeftBtn ?? false
            
        }
    }
    
    var hidRightBtn:Bool? {
        didSet {
            self.navigationItem.rightBarButtonItem?.customView?.isHidden = hidRightBtn ?? false
        }
    }

    var leftBtnTitle:String? {
        didSet {
            if let title = leftBtnTitle {
                leftBarButton.setTitle(title, for: .normal)
                self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: leftBarButton)
            }
        }
    }
    
    var rightBtnTitle:String? {
        didSet {
            if let title = rightBtnTitle {
                self.rightBarButton.setTitle(title, for: .normal)
                self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: self.rightBarButton)
            }
        }
    }
    
    var leftBtnImg:String? {
        didSet {
            if let title = leftBtnImg {
                leftBarButton.setImage(UIImage.init(named: title), for: UIControl.State.normal)
                self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: leftBarButton)
            }
        }
    }
    
    var rightBtnImg:String? {
        didSet {
            if let title = rightBtnImg {
                rightBarButton.setImage(UIImage.init(named: title), for: UIControl.State.normal)
                self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: rightBarButton)
            }
        }
    }
    
    
    // - 导航栏左边按钮
    private lazy var leftBarButton:UIButton = {
        let button = UIButton.init(type: UIButton.ButtonType.custom)
        button.frame = CGRect(x:0, y:0, width:30, height: 30)
        button.setImage(UIImage(named: NAVI_BACK_IMAGE), for: UIControl.State.normal)
        button.addTarget(self, action: #selector(leftBarButtonClick), for: UIControl.Event.touchUpInside)
        return button
    }()
    // - 导航栏右边按钮
    private lazy var rightBarButton:UIButton = {
        let button = UIButton.init(type: UIButton.ButtonType.custom)
        button.frame = CGRect(x:0, y:0, width:30, height: 30)
//        button.setImage(UIImage(named: ""), for: UIControl.State.normal)
        button.addTarget(self, action: #selector(rightBarButtonClick), for: UIControl.Event.touchUpInside)
        return button
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.interactivePopGestureRecognizer?.delegate = self
        navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        setNavigationBarView()
        setViewUI()
        loadData()
    }
        
    func setNavigationBarView() {
        // 导航栏左右item
       self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: leftBarButton)

    }
    
    func setViewUI() {
        
    }
    
    func loadData() {
        
    }
    
    // - 导航栏左边消息点击事件
    @objc func leftBarButtonClick() {
        if (self.presentingViewController != nil) {
            self.dismiss(animated: true, completion: nil)
        }
        else  {
            self.navigationController?.popViewController(animated: true)
        }
    }
    // - 导航栏左边消息点击事件
    @objc func rightBarButtonClick() {
        
    }
    
    deinit {
        DDLogInfo("释放==》\(self)")
    }
}
